package customview.ike.com.customviewproject.glide;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;

import java.io.InputStream;

import okhttp3.OkHttpClient;

/**
 *@author:ike
 *@function: 自定义Glide网络组件加载模块
 *@creat time:17:42 2018/2/12
 **/

public class OKHttpGlideUrlLoader implements ModelLoader<GlideUrl,InputStream> {
    private OkHttpClient okHttpClient;
    @Override
    public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) {
        return new OkHttpFetcher(okHttpClient,model);
    }
    public static class Factory implements ModelLoaderFactory<GlideUrl,InputStream> {

        private OkHttpClient client;
        private synchronized OkHttpClient getClient(){
            if (client==null){
                client=new OkHttpClient();
            }
            return client;
        } 
        @Override
        public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
            return new OKHttpGlideUrlLoader(getClient());
        }

        @Override
        public void teardown() {

        }
    }

    public OKHttpGlideUrlLoader(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }
}
